﻿using System;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI.WebControls;

public partial class hospital_search : System.Web.UI.Page
{
    // creating an instance of manager class
    public string search_type="";
    protected void Page_Load(object sender, EventArgs e)
    {
        //Page.Form.Attributes.Add("onSubmit", "javascript:showWorking()");
        //Master.keywordsContent = "top hospitals, hospitals online,  hospital list, search hospitals, board certified hospitals, top hospitals in United States";
        //Master.descriptionContent = "Findadoc.com offers top hospitals online, best hospitals list finder. You can find a hospital according to your requirements easily.";
        if (Request.QueryString["t"] != null)
        {
            if (Request.QueryString["t"].ToString().ToLower() == "usa")
            {
                Master.titleContent = "USA Hospital Search Results";
                header_label.InnerText = "USA Hospital Search";
                lbl_state.InnerText = "State";
                search_type = "usa";

            }
            else
            {
                Master.titleContent = "International Hospital Search Results";
                header_label.InnerText = "International Hospital Search";
                lbl_state.InnerText = "Country";
                search_type = "int";
            }
        }
        else { Master.titleContent = "Hospital Search Results";
        header_label.InnerText = "Hospital Search";
        }

        Master.TdRight = false;

        Master.TopLoginIMG = false;


        if (!Page.IsPostBack)
        {
            BindSearchData();
            BindTopHospitals();
        }



    }

    private void BindTopHospitals()
    {
        try
        {
            var ds = (System.Data.DataTable)Session["Top_Hospitals"];
            Top_Hospital_Repeater.DataSource = ds;
            Top_Hospital_Repeater.DataBind();
        }
        catch
        {

        }
    }

    private void BindSearchData()
    {
        var country_str = "";
        var state_str = "";
        System.Data.DataSet ds = new System.Data.DataSet();
        if (Request.QueryString["t"] != null)
        {
            if (Request.QueryString["t"].ToString().ToLower() == "usa")
            {
                if (Request.QueryString["s"] != null)
                {
                    //state_str = "'" + Request.QueryString["s"].ToString() + "'";
                    state_str = "" + Request.QueryString["s"].ToString() + "";
                    Master.titleContent = "USA Hospitals Search Results";
                    lblheadr.InnerText = "USA Hospitals Search Results";
                }
                else
                {
                    state_str = Session["usa_hospital_search_state"] != null ? Session["usa_hospital_search_state"].ToString() : " ";
                    if (state_str.Trim() != "")
                    {
                        state_str = "" + state_str + "";
                        //if (state_str.Contains(","))
                        //{
                        //    state_str = "'" + state_str.Replace(",", "','") + "'";
                        //}
                        //else
                        //{
                        //    state_str = "'" + state_str + "'";
                        //}
                    }
                }

                var sqlparam = new SqlParameter[]
                   {
           
                        new SqlParameter("@name", Session["usa_hospital_search_name"] != null ? Session["usa_hospital_search_name"].ToString() : " "),
                        new SqlParameter("@address",Session["usa_hospital_search_address"] != null ? Session["usa_hospital_search_address"].ToString() : " " ),
                        new SqlParameter("@state",state_str),
                        new SqlParameter("@zipcode",Session["usa_hospital_search_zipcode"] != null ? Session["usa_hospital_search_zipcode"].ToString() : " " ),
                    };
                ds = CustomRepository.CustumDataSetFunction("USP_USA_GET_SEARCHED_HOSPITALS", sqlparam);
            }
            else
            {
                if (Request.QueryString["c"] != null)
                {
                    country_str = "" + Request.QueryString["c"].ToString().Replace("-", " ") + "";
                    Master.titleContent = "Hospitals Search Results For " + (CommonFunction.ToTitleCase(Request.QueryString["c"].ToString().Replace("-", " "))).ToUpper();
                    lblheadr.InnerText = "Hospitals Search Results For " + (CommonFunction.ToTitleCase(Request.QueryString["c"].ToString().Replace("-", " "))).ToUpper();
                }
                else
                {
                    country_str = Session["int_hospital_search_country"] != null ? Session["int_hospital_search_country"].ToString() : " ";
                    if (country_str.Trim() != "")
                    {

                        //if (country_str.Contains(","))
                        //{
                        //    country_str = "'" + country_str.Replace(",", "','") + "'";
                        //}
                        //else
                        //{
                            country_str = "" + country_str + "";
                        //}
                    }
                }

                var sqlparam = new SqlParameter[]
                   {
           
                        new SqlParameter("@name", Session["int_hospital_search_name"] != null ? Session["int_hospital_search_name"].ToString() : " "),
                        new SqlParameter("@address",Session["int_hospital_search_address"] != null ? Session["int_hospital_search_address"].ToString() : " " ),
                        new SqlParameter("@country",country_str),
                        new SqlParameter("@zipcode",Session["int_hospital_search_zipcode"] != null ? Session["int_hospital_search_zipcode"].ToString() : " " ),
                    };
                ds = CustomRepository.CustumDataSetFunction("USP_GET_SEARCHED_HOSPITALS", sqlparam);

            }
        }




        grdHospitals.DataSource = ds.Tables[0];
        grdHospitals.DataBind();

        chk_Country.DataSource = ds.Tables[1];
        chk_Country.DataBind();

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string name = "", address = "", country = "", zipcode = "";

        if (txtname.Value.Trim().Replace("Search", "") != "")
        {
            name = txtname.Value.Trim();
        }
        if (txtaddress.Value.Trim() != "")
        {
            address = txtaddress.Value.Trim();
        }

        var s = chk_Country.Items.Cast<ListItem>()
                    .Where(item => item.Selected)
                    .Aggregate("", (current, item) => current + (item.Value + ", "));


        if (txtZipcode.Value.Trim() != "")
        {
            zipcode = txtZipcode.Value.Trim();
        }
        if (Request.QueryString["t"] != null)
        {
            if (Request.QueryString["t"].ToString().ToLower() == "usa")
            {
                Session["usa_hospital_search_name"] = name;
                Session["usa_hospital_search_address"] = address;
                Session["usa_hospital_search_state"] = s.TrimEnd(new[] { ',', ' ' });
                Session["usa_hospital_search_zipcode"] = zipcode;
                Response.Redirect(Session["strHttp"] + "hospital_search.aspx?t=usa");
            }
            else
            {
                Session["int_hospital_search_name"] = name;
                Session["int_hospital_search_address"] = address;
                Session["int_hospital_search_country"] = s.TrimEnd(new[] { ',', ' ' });
                Session["int_hospital_search_zipcode"] = zipcode;
                Response.Redirect(Session["strHttp"] + "hospital_search.aspx?t=int");
            }

        }

    }
    protected void grdHospitals_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        grdHospitals.PageIndex = e.NewPageIndex;
        BindSearchData();
    }

}